//
//  TiJianRootViewController.m
//  安好
//
//  Created by qinlongqing on 14-8-25.
//  Copyright (c) 2014年 冯凯. All rights reserved.
//

#import "TiJianRootViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "PLView.h"
#import "TiJianListView.h"
#import "ShiLiTestViewController.h"
#import "XinLvTestViewController.h"
#import "RankingListViewController.h"
#import "SeJueTestViewController.h"
#import "SupplenessViewController.h"
#import "FistTestViewController.h"



NSObject <PLIPanorama> *panorama_1 = nil;
NSObject <PLIPanorama> *panorama_2 = nil;


@interface TiJianRootViewController ()<PLViewDelegate>
{
    AVAudioPlayer *audioPlayer;
    
    PLView *plView_1;
    PLView *plView_2;
    
    UIButton *listButton;
    UIButton *backButton;
    
    TiJianListView *tiJianListView;
    
    int page;//1.体检室  2.体能室

    BOOL isButton;//区分点击事件和3D转动事件

    double lastTime;
}
@end

@implementation TiJianRootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES];
    
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:NO];

    [super viewWillDisappear:animated];
}

//
//-(void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    
//}
//
//
//-(void)viewDidDisappear:(BOOL)animated
//{
//	[super viewDidDisappear:animated];
//}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (IOS7_OR_LATER) {
        
    }
    else{
        CGRect rect = self.view.frame;
        rect.origin.y = 0;
        self.view.frame = rect;
    }
    
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Exclamation.wav",[[NSBundle mainBundle] resourcePath]]];
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    audioPlayer.numberOfLoops = 1;
    
    plView_1 = [[PLView alloc]initWithFrame:self.view.frame];
    plView_1.delegate = self;
    [self.view addSubview:plView_1];
    
    panorama_1 = [PLSpherical2Panorama panorama];
    [panorama_1.currentCamera lookAtWithPitch:0 yaw:-55];
    [(PLSpherical2Panorama *)panorama_1 setImage:[PLImage imageWithPath:[[NSBundle mainBundle]pathForResource:@"TIJIAN" ofType:@"jpg"]]];
    [self setTJButton];
    
    [plView_1 setPanorama:panorama_1];//执行慢
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        plView_2 = [[PLView alloc]initWithFrame:self.view.frame];
        plView_2.delegate = self;
        
        panorama_2 = [PLSpherical2Panorama panorama];
        [(PLSpherical2Panorama *)panorama_2 setImage:[PLImage imageWithPath:[[NSBundle mainBundle] pathForResource:@"TINENG" ofType:@"jpg"]]];
        [self setTNButton];
        
        [plView_2 setPanorama:panorama_2];
    });
    
    NSArray *components = [[NSBundle mainBundle] loadNibNamed:@"TiJianListView" owner:self options:nil];
    listButton = [components objectAtIndex:1];
    [listButton setCenter:CGPointMake(self.view.frame.size.width - 50, self.view.frame.origin.y + 45)];
    [listButton addTarget:self action:@selector(listButtonDidTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:listButton];
    
    
    backButton = [components objectAtIndex:2];
    [backButton setCenter:CGPointMake(50, self.view.frame.origin.y + 45)];
    [backButton addTarget:self action:@selector(backDidTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    
    tiJianListView = [components objectAtIndex:0];
    [tiJianListView setCenter:self.view.center];
    tiJianListView.tijianRootViewController = self;
    
    page = 1;
    
    [self listButtonDidTouch:listButton];
    
////    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
////    }
}

- (void)setTJButton{
    NSData *dataStr = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"buttons" ofType:@"data"]];
    NSDictionary *dataDict = [dataStr objectFromJSONData];
    NSArray *dataArray = [dataDict objectForKey:@"tijian"];
    
    for (NSDictionary *item in dataArray) {
        NSString *imgStr = [item objectForKey:@"image"];
        int _x = [(NSString *)[item objectForKey:@"x"] intValue];
        int _y = [(NSString *)[item objectForKey:@"y"] intValue];
        
        int x = 360 * _x / 2048 - 180;
        int y = 90 - 180 * _y / 1024;
        
        UIImage *image = [UIImage imageNamed:imgStr];
        int _width = image.size.width;
        int _height = image.size.height;
        
        float width = (float)_width / 2048;
        float height = (float)_height / 1024;
        
        NSString *path = [[NSBundle mainBundle] pathForResource:imgStr ofType:@"png"];
        if (path) {
            PLTexture *hotspotTexture = [PLTexture textureWithImage:[PLImage imageWithPath:path]];
            if ([imgStr isEqualToString:@"EXIT门"]) {
                PLHotspot *hotspot = [PLHotspot hotspotWithId:1 texture:hotspotTexture atv:y ath:x width:width*3 height:height*1.5];
                [panorama_1 addHotspot:hotspot];
            }
            else if ([imgStr isEqualToString:@"去体能室的门"]){
                PLHotspot *hotspot = [PLHotspot hotspotWithId:2 texture:hotspotTexture atv:y ath:x width:width*3 height:height*1.5];
                [panorama_1 addHotspot:hotspot];
            }
            else if ([imgStr isEqualToString:@"视力表"]){
                PLHotspot *hotspot = [PLHotspot hotspotWithId:3 texture:hotspotTexture atv:y ath:x width:width*3 height:height*1.5];
                [panorama_1 addHotspot:hotspot];
            }
            else if ([imgStr isEqualToString:@"听诊器"]){
                PLHotspot *hotspot = [PLHotspot hotspotWithId:4 texture:hotspotTexture atv:y ath:x width:width*3 height:height*1.5];
                [panorama_1 addHotspot:hotspot];
            }
            else if ([imgStr isEqualToString:@"奖杯"]){
                PLHotspot *hotspot = [PLHotspot hotspotWithId:5 texture:hotspotTexture atv:y ath:x width:width*3 height:height*1.5];
                [panorama_1 addHotspot:hotspot];
            }
            else if ([imgStr isEqualToString:@"色盲检查图"]){
                PLHotspot *hotspot = [PLHotspot hotspotWithId:6 texture:hotspotTexture atv:y ath:x width:width*3 height:height*1.5];
                [panorama_1 addHotspot:hotspot];
            }
            else{
                
                
            }
        }
    }
}

- (void)setTNButton{
    NSData *dataStr = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"buttons" ofType:@"data"]];
    NSDictionary *dataDict = [dataStr objectFromJSONData];
    NSArray *dataArray = [dataDict objectForKey:@"tineng"];
    
    for (NSDictionary *item in dataArray) {
        NSString *imgStr = [item objectForKey:@"image"];
        int _x = [(NSString *)[item objectForKey:@"x"] intValue];
        int _y = [(NSString *)[item objectForKey:@"y"] intValue];
        int x = 360 * _x /2048 - 180;
        int y = 90 - 180 * _y / 1024;
        
        UIImage *image = [UIImage imageNamed:imgStr];
        int _width = image.size.width;
        int _height = image.size.height;
        float width = (float)_width / 2048;
        float height = (float)_height / 1024;
        
        NSString *path = [[NSBundle mainBundle] pathForResource:imgStr ofType:@"png"];
        if (path) {
            PLTexture *hotspotTexture = [PLTexture textureWithImage:[PLImage imageWithPath:path]];
            if ([imgStr isEqualToString:@"去往体检室的门"]) {
                PLHotspot *hotspot = [PLHotspot hotspotWithId:100 texture:hotspotTexture atv:y ath:x width:width*3 height:height*1.5];
                [panorama_2 addHotspot:hotspot];
            }
            else if ([imgStr isEqualToString:@"挥拳"]) {
                PLHotspot *hotspot = [PLHotspot hotspotWithId:101 texture:hotspotTexture atv:y ath:x width:width*3 height:height*1.5];
                [panorama_2 addHotspot:hotspot];
            }
            else if ([imgStr isEqualToString:@"柔韧性"]) {
                PLHotspot *hotspot = [PLHotspot hotspotWithId:102 texture:hotspotTexture atv:y ath:x width:width*3 height:height*1.5];
                [panorama_2 addHotspot:hotspot];
            }
            
        }
    }
}

- (void)view:(UIView<PLIView> *)pView didClickHotspot:(PLHotspot *)hotspot screenPoint:(CGPoint)point scene3DPoint:(PLPosition)position{
    
    MyNSLog("dsdds   fds   %lld",hotspot.identifier);
    if (hotspot.identifier == 1) {
        MyNSLog("点击了exit门");
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    if (hotspot.identifier == 2) {
        MyNSLog("去体能室");
        [self.view insertSubview:plView_2 belowSubview:listButton];
        
        page = 2;
    }
    if (hotspot.identifier == 3) {
        MyNSLog("点击了视力");
        ShiLiTestViewController *shiLiTest = [[ShiLiTestViewController alloc]init];
        [self.navigationController pushViewController:shiLiTest animated:YES];
    }
    if (hotspot.identifier == 4) {
        MyNSLog("点击了听诊");
        XinLvTestViewController *xinLv = [[XinLvTestViewController alloc]init];
        [self.navigationController pushViewController:xinLv animated:YES];
    }
    if (hotspot.identifier == 5) {
        MyNSLog("点击了奖杯");
        RankingListViewController *rankingListViewController = [[RankingListViewController alloc]init];
        [self.navigationController pushViewController:rankingListViewController animated:YES];
    }
    if (hotspot.identifier == 6) {
        MyNSLog("点击了色觉测试");
        SeJueTestViewController *sejueTestViewController = [[SeJueTestViewController alloc]init];
        [self.navigationController pushViewController:sejueTestViewController animated:YES];
    }
    if (hotspot.identifier == 100) {
        MyNSLog("去往体检室的门");
        [plView_2 removeFromSuperview];
        
        page = 1;
    }
    if (hotspot.identifier == 101) {
        FistTestViewController *fistTest = [[FistTestViewController alloc]initWithNibName:@"FistTestViewController" bundle:nil];
        [self.navigationController pushViewController:fistTest animated:YES];
    }
    if (hotspot.identifier == 102) {
        SupplenessViewController *supplenessTest = [[SupplenessViewController alloc]initWithNibName:@"SupplenessViewController" bundle:nil];
        [self.navigationController pushViewController:supplenessTest animated:YES];
    }
    isButton = YES;
}

- (void)view:(UIView<PLIView> *)pView touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

}

- (void)view:(UIView<PLIView> *)pView touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    isButton = YES;
}

- (void)view:(UIView<PLIView> *)pView touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
}

- (void)view:(UIView<PLIView> *)pView didBeginTouching:(NSSet *)touches withEvent:(UIEvent *)event{
    lastTime = [[NSDate date] timeIntervalSince1970];
    isButton = NO;
}

- (void)view:(UIView<PLIView> *)pView didTouch:(NSSet *)touches withEvent:(UIEvent *)event{
    
}

- (void)view:(UIView<PLIView> *)pView didEndTouching:(NSSet *)touches withEvent:(UIEvent *)event{
    double nowTime = [[NSDate date] timeIntervalSince1970];
    
    if (!isButton) {
        if ((nowTime - lastTime) < 0.5) {
            if (audioPlayer) {
                [audioPlayer play];
            }
        }
    }
    lastTime = nowTime;
}

- (void)view:(UIView<PLIView> *)pView didBeginZooming:(CGPoint)starPoint endPoint:(CGPoint)endPoint{
    isButton = YES;
}

- (void)view:(UIView<PLIView> *)pView didRunZooming:(float)distance isZoomIn:(BOOL)isZoomIn isZoomOut:(BOOL)isZoomOut{
    isButton = YES;
}

- (void)view:(UIView<PLIView> *)pView didEndZooming:(float)distance isZoomIn:(BOOL)isZoomIn isZoomOut:(BOOL)isZoomOut{
    isButton = YES;
}

- (void)listButtonDidTouch:(UIButton *)sender{
    NSArray *array = [self.view subviews];
    int num = 0;
    for (UIView *item in array) {
        if (item == tiJianListView) {
            num = 1;
            break;
        }
    }
    if (num == 1) {
        [tiJianListView removeFromSuperview];
    }
    else{
        listButton.hidden = YES;
        backButton.hidden = YES;
        [tiJianListView showWithCenter:self.view.center];
    }
}

- (void)backDidTouch:(UIButton *)sender{
    if (page == 1) {
        MyNSLog("你现在在体检室");
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if (page == 2){
        MyNSLog("你刚才在体能室");
        [plView_2 removeFromSuperview];
        
        page = 1;
    }
}

- (void)listDisappear{
    listButton.hidden = NO;
    backButton.hidden = NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
